package com.cs.framework.ui.builder.db;

import com.cs.framework.common.util.StringUtils;
import com.cs.framework.ui.exception.UIExceptionHelper;
import com.cs.framework.ui.model.PageInfo;
import com.cs.framework.ui.model.PanelInfo;
import com.cs.framework.ui.model.UIInfo;

/**
 * 构建页面
 * 
 * @author hedongzhou
 * 
 */
public class PageBuilder extends AbstractDbUIBuilder {

	/**
	 * 构建面板
	 */
	protected PanelBuilder panelBuilder;

	public PanelBuilder getPanelBuilder() {
		return panelBuilder;
	}

	/**
	 * 初始化
	 */
	public void init() {
		super.init();
		panelBuilder = new PanelBuilder();
		panelBuilder.setDefDao(defDao);
		panelBuilder.init();
	}

	/**
	 * 构建页面
	 * 
	 * @param pageId
	 *            页面ID
	 * @param superId
	 *            父控件ID（无用，传空）
	 * @return
	 */
	public UIInfo build(String pageId, String superId) {
		// 查询页面
		PageInfo page = defDao.getPage(pageId, PageInfo.class);
		if (page == null) {
			UIExceptionHelper.throwUIException("UI004", pageId);
		}

		if (StringUtils.isNotBlank(page.getPanelId())) {
			// 获取面板信息
			page.setPanel((PanelInfo) panelBuilder.build(page.getPanelId(), null));
		}

		return page;
	}

}
